<?php
$pghostSource="host=www.south.rmutsv.ac.th port=5432 dbname=edoc110 user=mooz password=moo2520";
//$pghostSource="host=localhost port=5432 dbname=edoc user=mooz password=moo2520";
$pghostDest="host=localhost port=5432 dbname=edoc_back1 user=mooz password=moo2520";

function updatePG($query,$dbx){
	$dbconn = pg_connect($dbx)
		or die('Could not connect: ' . pg_last_error());
		
	$result = pg_exec($dbconn,$query);

	pg_close($dbconn);
	
	return $result;
}

function create_edoc_db($desk){
    $q = "--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--

COMMENT ON SCHEMA public IS 'Standard public schema';


SET search_path = public, pg_catalog;

--
-- Name: depart_recieve_seq_1; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE depart_recieve_seq_1
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.depart_recieve_seq_1 OWNER TO mooz;

SET default_tablespace = '';

SET default_with_oids = false;

--
-- Name: department; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE department (
    depart_id integer NOT NULL,
    depart_name character varying NOT NULL
);


ALTER TABLE public.department OWNER TO mooz;

--
-- Name: department_depart_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE department_depart_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.department_depart_id_seq OWNER TO mooz;

--
-- Name: department_hier; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE department_hier (
    head_depart_id integer NOT NULL,
    under_depart_id integer NOT NULL
);


ALTER TABLE public.department_hier OWNER TO mooz;

--
-- Name: edoc; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE edoc (
    edoc_id bigint NOT NULL,
    doc_no character varying NOT NULL,
    doc_date date NOT NULL,
    sender character varying,
    reciever character varying,
    headline character varying,
    \"comment\" character varying,
    depart_send_id integer NOT NULL,
    depart_send_name_out character varying,
    secret smallint NOT NULL,
    rapid smallint NOT NULL,
    pdf bytea,
    pdf_name character varying,
    pdf_type character varying,
    depart_owner_id integer NOT NULL
);


ALTER TABLE public.edoc OWNER TO mooz;

--
-- Name: edoc_edoc_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE edoc_edoc_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.edoc_edoc_id_seq OWNER TO mooz;

--
-- Name: edoc_recieve; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE edoc_recieve (
    edoc_type_id integer NOT NULL,
    recieve_no character varying NOT NULL,
    recieve_date date DEFAULT now() NOT NULL,
    recieve_time time without time zone DEFAULT now() NOT NULL,
    edoc_id bigint NOT NULL
);


ALTER TABLE public.edoc_recieve OWNER TO mooz;

--
-- Name: edoc_run; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE edoc_run (
    edoc_id bigint NOT NULL,
    depart_id integer NOT NULL
);


ALTER TABLE public.edoc_run OWNER TO mooz;

--
-- Name: edoc_type; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE edoc_type (
    edoc_type_id integer NOT NULL,
    edoc_type_name character varying NOT NULL,
    depart_id integer NOT NULL
);


ALTER TABLE public.edoc_type OWNER TO mooz;

--
-- Name: edoc_type_edoc_type_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE edoc_type_edoc_type_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.edoc_type_edoc_type_id_seq OWNER TO mooz;

--
-- Name: edoc_user; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE edoc_user (
    user_id integer NOT NULL,
    user_name character varying NOT NULL,
    user_passwd character varying,
    \"read\" smallint NOT NULL,
    \"write\" smallint NOT NULL,
    user_type smallint NOT NULL,
    secret smallint NOT NULL,
    depart_id integer NOT NULL
);


ALTER TABLE public.edoc_user OWNER TO mooz;

--
-- Name: edoc_user_user_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE edoc_user_user_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.edoc_user_user_id_seq OWNER TO mooz;

--
-- Name: ksean; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE ksean (
    ksean_id bigint NOT NULL,
    edoc_id bigint NOT NULL,
    pname_id integer NOT NULL,
    ksean_txt character varying,
    ksean_date date NOT NULL
);


ALTER TABLE public.ksean OWNER TO mooz;

--
-- Name: ksean_ksean_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE ksean_ksean_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.ksean_ksean_id_seq OWNER TO mooz;

--
-- Name: log; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE log (
    log_id bigint NOT NULL,
    user_id integer NOT NULL,
    user_name character varying NOT NULL,
    operation character varying NOT NULL,
    detail character varying NOT NULL,
    \"time\" time without time zone DEFAULT now() NOT NULL,
    depart_id integer NOT NULL,
    depart_name character varying NOT NULL
);


ALTER TABLE public.log OWNER TO mooz;

--
-- Name: log_log_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE log_log_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.log_log_id_seq OWNER TO mooz;

--
-- Name: pname; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE pname (
    pname_id integer NOT NULL,
    pname_name character varying NOT NULL,
    position_id integer NOT NULL,
    pname_status boolean DEFAULT true NOT NULL
);


ALTER TABLE public.pname OWNER TO mooz;

--
-- Name: pname_pname_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE pname_pname_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.pname_pname_id_seq OWNER TO mooz;

--
-- Name: position; Type: TABLE; Schema: public; Owner: mooz; Tablespace: 
--

CREATE TABLE \"position\" (
    position_id integer NOT NULL,
    position_name character varying NOT NULL,
    depart_id integer NOT NULL
);


ALTER TABLE public.\"position\" OWNER TO mooz;

--
-- Name: position_position_id_seq; Type: SEQUENCE; Schema: public; Owner: mooz
--

CREATE SEQUENCE position_position_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


ALTER TABLE public.position_position_id_seq OWNER TO mooz;

--
-- Name: view_depart_hier; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_depart_hier AS
    SELECT department_hier.head_depart_id, head_depart.depart_name AS head_name, department_hier.under_depart_id, under_depart.depart_name AS under_name FROM ((department_hier JOIN department head_depart ON ((department_hier.head_depart_id = head_depart.depart_id))) JOIN department under_depart ON ((department_hier.under_depart_id = under_depart.depart_id)));


ALTER TABLE public.view_depart_hier OWNER TO mooz;

--
-- Name: view_edoc; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_run AS
    SELECT department.depart_name, department.depart_id, edoc_run.edoc_id FROM (edoc_run JOIN department ON ((edoc_run.depart_id = department.depart_id)));


ALTER TABLE public.view_edoc_run OWNER TO mooz;


CREATE VIEW view_edoc AS
    SELECT edoc.edoc_id, edoc.doc_no, edoc.doc_date, edoc.sender, edoc.reciever, edoc.headline, edoc.\"comment\", edoc.depart_send_id, edoc.depart_send_name_out, depart_edoc.depart_name AS depart_name_send, edoc.secret, edoc.rapid, edoc.pdf_name, edoc.pdf_type, edoc.depart_owner_id, depart_owner.depart_name AS depart_name_owner FROM ((edoc JOIN department depart_edoc ON ((depart_edoc.depart_id = edoc.depart_send_id))) JOIN department depart_owner ON ((depart_owner.depart_id = edoc.depart_owner_id)));


ALTER TABLE public.view_edoc OWNER TO mooz;

--
-- Name: view_edoc_pdf; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_pdf AS
    SELECT edoc.pdf, edoc.edoc_id, edoc.pdf_name, edoc.pdf_type FROM edoc;


ALTER TABLE public.view_edoc_pdf OWNER TO mooz;

--
-- Name: view_edoc_recieve; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_recieve AS
    SELECT edoc_recieve.recieve_no, edoc_recieve.recieve_date, edoc_recieve.recieve_time, edoc_recieve.edoc_id, edoc_recieve.edoc_type_id, edoc_type.edoc_type_name, edoc_type.depart_id, department.depart_name FROM (edoc_recieve JOIN (edoc_type JOIN department ON ((edoc_type.depart_id = department.depart_id))) ON ((edoc_recieve.edoc_type_id = edoc_type.edoc_type_id)));


ALTER TABLE public.view_edoc_recieve OWNER TO mooz;

--
-- Name: view_edoc_search; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_search AS
    SELECT edoc.edoc_id, edoc.doc_no, edoc.doc_date, edoc.sender, edoc.reciever, edoc.headline, edoc.\"comment\", edoc.depart_send_id, edoc.depart_send_name_out, depart_edoc.depart_name AS depart_name_send, edoc.secret, edoc.rapid, edoc.pdf_name, edoc.pdf_type, edoc.depart_owner_id, depart_owner.depart_name AS depart_name_owner, edoc_recieve.recieve_no, edoc_recieve.recieve_date, edoc_recieve.recieve_time, edoc_recieve.edoc_type_id, edoc_type.edoc_type_name, edoc_type.depart_id AS depart_id_recieve, depart_recieve.depart_name AS depart_name_recieve FROM (((edoc JOIN department depart_edoc ON ((depart_edoc.depart_id = edoc.depart_send_id))) JOIN department depart_owner ON ((depart_owner.depart_id = edoc.depart_owner_id))) JOIN (edoc_recieve JOIN (edoc_type JOIN department depart_recieve ON ((edoc_type.depart_id = depart_recieve.depart_id))) ON ((edoc_recieve.edoc_type_id = edoc_type.edoc_type_id))) ON ((edoc.edoc_id = edoc_recieve.edoc_id)));


ALTER TABLE public.view_edoc_search OWNER TO mooz;

--
-- Name: view_edoc_search_ksean; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_search_ksean AS
    SELECT edoc.edoc_id, ksean.ksean_id, ksean.pname_id FROM (edoc JOIN ksean ON ((edoc.edoc_id = ksean.edoc_id)));


ALTER TABLE public.view_edoc_search_ksean OWNER TO mooz;

--
-- Name: view_edoc_type; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_edoc_type AS
    SELECT department.depart_name, edoc_type.edoc_type_id, edoc_type.edoc_type_name, edoc_type.depart_id FROM (edoc_type JOIN department ON ((edoc_type.depart_id = department.depart_id)));


ALTER TABLE public.view_edoc_type OWNER TO mooz;

--
-- Name: view_ksean; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_ksean AS
    SELECT ksean.ksean_date, ksean.edoc_id, ksean.ksean_id, ksean.pname_id, ksean.ksean_txt, pname.pname_name, pname.position_id, \"position\".position_name, department.depart_id, department.depart_name FROM ((ksean JOIN pname ON ((ksean.pname_id = pname.pname_id))) JOIN (\"position\" JOIN department ON ((\"position\".depart_id = department.depart_id))) ON ((pname.position_id = \"position\".position_id)));


ALTER TABLE public.view_ksean OWNER TO mooz;

--
-- Name: view_pname; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_pname AS
    SELECT department.depart_name, pname.pname_name, pname.position_id, pname.pname_status, pname.pname_id, \"position\".position_name, \"position\".depart_id FROM ((pname JOIN \"position\" ON ((pname.position_id = \"position\".position_id))) JOIN department ON ((\"position\".depart_id = department.depart_id)));


ALTER TABLE public.view_pname OWNER TO mooz;

--
-- Name: view_position; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_position AS
    SELECT department.depart_name, \"position\".position_id, \"position\".position_name, \"position\".depart_id FROM (\"position\" JOIN department ON ((\"position\".depart_id = department.depart_id)));


ALTER TABLE public.view_position OWNER TO mooz;

--
-- Name: view_run_recieve; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_run_recieve AS
    SELECT edoc.edoc_id, edoc.doc_no, edoc.doc_date, edoc.sender, edoc.reciever, edoc.headline, edoc.\"comment\", edoc.depart_send_id, edoc.depart_send_name_out, depart_edoc.depart_name AS depart_name_send, edoc.secret, edoc.rapid, edoc.pdf_name, edoc.pdf_type, edoc.depart_owner_id, depart_owner.depart_name AS depart_name_owner, edoc_recieve.recieve_no, edoc_recieve.recieve_date, edoc_recieve.recieve_time, edoc_recieve.edoc_type_id, edoc_type.edoc_type_name, edoc_type.depart_id AS depart_id_recieve, depart_recieve.depart_name AS depart_name_recieve, edoc_run.depart_id AS depart_id_run, depart_run.depart_name AS depart_run FROM ((((edoc JOIN department depart_edoc ON ((depart_edoc.depart_id = edoc.depart_send_id))) JOIN department depart_owner ON ((depart_owner.depart_id = edoc.depart_owner_id))) JOIN (edoc_run JOIN department depart_run ON ((edoc_run.depart_id = depart_run.depart_id))) ON ((edoc_run.edoc_id = edoc.edoc_id))) JOIN (edoc_recieve JOIN (edoc_type JOIN department depart_recieve ON ((edoc_type.depart_id = depart_recieve.depart_id))) ON ((edoc_recieve.edoc_type_id = edoc_type.edoc_type_id))) ON ((edoc.edoc_id = edoc_recieve.edoc_id)));


ALTER TABLE public.view_run_recieve OWNER TO mooz;

--
-- Name: view_user; Type: VIEW; Schema: public; Owner: mooz
--

CREATE VIEW view_user AS
    SELECT department.depart_name, edoc_user.user_passwd, edoc_user.\"read\", edoc_user.\"write\", edoc_user.secret, edoc_user.user_id, edoc_user.depart_id, edoc_user.user_name, edoc_user.user_type FROM (edoc_user JOIN department ON ((edoc_user.depart_id = department.depart_id)));


ALTER TABLE public.view_user OWNER TO mooz;

--
-- Name: department_depart_name_key; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY department
    ADD CONSTRAINT department_depart_name_key UNIQUE (depart_name);


--
-- Name: department_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY department
    ADD CONSTRAINT department_pkey PRIMARY KEY (depart_id);


--
-- Name: edoc_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY edoc
    ADD CONSTRAINT edoc_pkey PRIMARY KEY (edoc_id);


--
-- Name: edoc_type_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY edoc_type
    ADD CONSTRAINT edoc_type_pkey PRIMARY KEY (edoc_type_id);


--
-- Name: edoc_user_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY edoc_user
    ADD CONSTRAINT edoc_user_pkey PRIMARY KEY (user_id);


--
-- Name: edoc_user_user_name_key; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY edoc_user
    ADD CONSTRAINT edoc_user_user_name_key UNIQUE (user_name);


--
-- Name: ksean_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY ksean
    ADD CONSTRAINT ksean_pkey PRIMARY KEY (ksean_id);


--
-- Name: log_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY log
    ADD CONSTRAINT log_pkey PRIMARY KEY (log_id);


--
-- Name: pname_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY pname
    ADD CONSTRAINT pname_pkey PRIMARY KEY (pname_id);


--
-- Name: position_pkey; Type: CONSTRAINT; Schema: public; Owner: mooz; Tablespace: 
--

ALTER TABLE ONLY \"position\"
    ADD CONSTRAINT position_pkey PRIMARY KEY (position_id);


--
-- Name: depart_owner_id; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc
    ADD CONSTRAINT depart_owner_id FOREIGN KEY (depart_owner_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: depart_send_id; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc
    ADD CONSTRAINT depart_send_id FOREIGN KEY (depart_send_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_recieve_edoc_id; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_recieve
    ADD CONSTRAINT edoc_recieve_edoc_id FOREIGN KEY (edoc_id) REFERENCES edoc(edoc_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_recieve_edoc_type; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_recieve
    ADD CONSTRAINT edoc_recieve_edoc_type FOREIGN KEY (edoc_type_id) REFERENCES edoc_type(edoc_type_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_run_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_run
    ADD CONSTRAINT edoc_run_depart FOREIGN KEY (depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_run_edoc; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_run
    ADD CONSTRAINT edoc_run_edoc FOREIGN KEY (edoc_id) REFERENCES edoc(edoc_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_type_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_type
    ADD CONSTRAINT edoc_type_depart FOREIGN KEY (depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: edoc_user_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY edoc_user
    ADD CONSTRAINT edoc_user_depart FOREIGN KEY (depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: head_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY department_hier
    ADD CONSTRAINT head_depart FOREIGN KEY (head_depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: ksean_edoc; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY ksean
    ADD CONSTRAINT ksean_edoc FOREIGN KEY (edoc_id) REFERENCES edoc(edoc_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: ksean_pname; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY ksean
    ADD CONSTRAINT ksean_pname FOREIGN KEY (pname_id) REFERENCES pname(pname_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: pname_position; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY pname
    ADD CONSTRAINT pname_position FOREIGN KEY (position_id) REFERENCES \"position\"(position_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: position_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY \"position\"
    ADD CONSTRAINT position_depart FOREIGN KEY (depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: under_depart; Type: FK CONSTRAINT; Schema: public; Owner: mooz
--

ALTER TABLE ONLY department_hier
    ADD CONSTRAINT under_depart FOREIGN KEY (under_depart_id) REFERENCES department(depart_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--
";

    updatePG($q,$dest);
}

function back_table($table,$source,$dest){
	$dbconn = pg_connect($source)
    	or die('Could not connect: ' . pg_last_error());

	$result = pg_query("select * from $table;") or die('Query failed: ' . pg_last_error());

    $i = 0;

	while($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
		$q_attr = "";
		$q_value = "";
		foreach($line as $attr => $value){
			if($q_attr!=""){
                $q_attr = $q_attr.",";
                $q_value = $q_value.",";
			}
			$q_attr = $q_attr.$attr;
            $q_value = $q_value."'".$value."'";
		}
        $q = "insert into $table (".$q_attr.") values (".$q_value.");";
        updatePG($q,$dest);

	}

    pg_free_result($result);
	pg_close($dbconn);

}

//create_edoc_db($pghostDest);
back_table("department",$pghostSource,$pghostDest);
back_table("department_hier",$pghostSource,$pghostDest);
//back_table("edoc_type",$pghostSource,$pghostDest);
//back_table("edoc_run",$pghostSource,$pghostDest);
//back_table("edoc",$pghostSource,$pghostDest);
back_table("position",$pghostSource,$pghostDest);
back_table("edoc_user",$pghostSource,$pghostDest);
//back_table("edoc_recieve",$pghostSource,$pghostDest);
back_table("pname",$pghostSource,$pghostDest);
//back_table("ksean",$pghostSource,$pghostDest);

?>
